package com.personnalManagerSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.personnalManagerSystem.model.Role;
import com.personnalManagerSystem.model.UserRole;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-03-27
 */
@Controller
@RequestMapping("/user-role")
public class UserRoleController  extends  BaseController{

    @RequestMapping("/FProle")
    @ResponseBody
    /**
     * @Description  用于角色模态框中该用户对应的角色的回显
     * @author liqing
     * @data 2022/3/30  20:03
     * @param userId
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String,Object> edit(Integer userId){
        System.out.println("userId="+userId);
        Map<String,Object> map = new HashMap<>();
        //根据userId拿到user-role中的关系记录
        List<UserRole> userRoleList = iUserRoleService.list(new QueryWrapper<UserRole>().eq("user_id", userId));
        //判断改用户是否在关系表中有记录,若无,则返回空ma'p
        if(userRoleList.size()<1){
            map.put("state","isNull");
            map.put("roleNameList","");
            return map;
        }

        //取出用户角色表中的roleId
        List<Integer> roleIdList = userRoleList.stream()
                .map(UserRole::getRoleId)
                .collect(Collectors.toList());
        //根据roleId信息在对应的role表中查出相关数据
        List<Role> roleList = iRoleService.list(new QueryWrapper<Role>().in("role_id", roleIdList));
        //取出role表中对应的roleName
        List<String> roleNameList = roleList.stream()
                .map(Role::getRoleName)
                .collect(Collectors.toList());
        //将对应userId对应的角色名存储在map集合中
        map.put("state","ok");
        map.put("roleNameList",roleNameList);
        return map;
    }
    @RequestMapping("/fpCommit")
    @ResponseBody
    public Map<String,Object>  fpCommit(@RequestParam("userId")Integer userId,
                                        @RequestParam("roleIds")List<String>roleIds){
        System.out.println("userId:"+userId);
        System.out.println("roleIds:"+roleIds);
        Map<String,Object> map = new HashMap<>();
        //清空用户角色表中与传入用户id相关的记录
        iUserRoleService.remove(new QueryWrapper<UserRole>().eq("user_id",userId));
        //将传入的用户id和角色id转换为用户角色关系对象,并存入用户角色表中
        ArrayList<UserRole> userRoleList = new ArrayList<>();
        for(String roleId:roleIds){
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(Integer.parseInt(roleId));
            userRoleList.add(userRole);
        }
        //将List集合存入数据库中
        iUserRoleService.saveOrUpdateBatch(userRoleList);
        map.put("code",200);
        return map;
     }



}

















































